import sys

from PyQt6 import QtWidgets
from PyQt6.QtWidgets import QGridLayout, QComboBox, QMessageBox

from uiimpl.timeEditImpl import TimeEditImpl
from common.dosvn import get_svn_version, get_server_info
from common.initpath import init_server_path
from common.dialogprocess import ProgressDialog


class UiServer(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        # 界面元素定义
        self.progress_dialog = None
        self.logcancel = QtWidgets.QTextBrowser()
        self.shutdown = QtWidgets.QPushButton('关服')
        self.restart = QtWidgets.QPushButton('重启')
        self.clearDB = QtWidgets.QPushButton('清档')
        self.server_version = QComboBox()  # 服务器 alpha release
        self.server_type = QComboBox()  # 服务器版本
        self.server_version_Specify = QtWidgets.QLineEdit()  # 服务器指定版本
        self.server_update = QtWidgets.QPushButton("更新")  # 服务器更新
        self.server_version_starting = QtWidgets.QLabel()  # 启动中服务器版本
        self.server_git_version = QtWidgets.QLabel()  # 启动中服务器git版本
        self.server_excel_version = QtWidgets.QLabel()  # 启动中服务器excel版本

        self.initui()
        self.connect_signal()

    def initui(self):
        grid = QGridLayout()
        grid.setSpacing(5)
        widget1 = TimeEditImpl()
        widget1.setMaximumSize(258, 99)
        widget1.setMinimumSize(258, 99)

        grid.addWidget(widget1, 0, 0, 3, 4)

        # 第一行组件
        self.server_type.addItems(['alpha', 'release'])
        grid.addWidget(self.server_type, 0, 4, 1, 1)
        self.server_version.addItems(['指定版本'])
        grid.addWidget(self.server_version, 0, 5, 1, 1)
        self.server_version_Specify.setMaximumWidth(70)
        grid.addWidget(self.server_version_Specify, 0, 6, 1, 1)
        grid.addWidget(self.server_update, 0, 7, 1, 1)
        grid.addWidget(QtWidgets.QLabel("当前版本:"), 0, 11, 1, 1)
        grid.addWidget(self.server_version_starting, 0, 12, 1, 1)

        # 第2行
        grid.addWidget(QtWidgets.QLabel("服务器操作:"), 1, 4, 1, 1)
        grid.addWidget(self.clearDB, 1, 5, 1, 1)
        grid.addWidget(self.restart, 1, 6, 1, 1)
        grid.addWidget(self.shutdown, 1, 7, 1, 1)
        grid.addWidget(QtWidgets.QLabel("git版本:"), 1, 11, 1, 1)
        grid.addWidget(self.server_git_version, 1, 12, 1, 1)

        # 第三行
        grid.addWidget(QtWidgets.QLabel("excel版本:"), 2, 11, 1, 1)
        grid.addWidget(self.server_excel_version, 2, 12, 1, 1)

        # 第6行
        grid.addWidget(self.logcancel, 5, 0, 15, 15)

        self.setLayout(grid)

        self.setWindowTitle('server')
        self.show()

    # 信号连接类
    def connect_signal(self):
        self.server_type.currentTextChanged.connect(self.select_server_type)
        self.server_version.currentTextChanged.connect(self.select_server_version)
        self.server_update.clicked.connect(lambda: self.show_progress_dialog(self.update_server))

    # 服务器alpha release选择
    def select_server_type(self):
        if self.server_type.currentText() == 'alpha':
            self.server_version.clear()
            self.server_version.addItems(['指定版本'])
        else:
            self.server_version.clear()
            self.server_version.addItems(
                get_svn_version(r'http://cd.devsvn.uuzuonline.com/svn/sango_server/ioe_server/release'))

    def select_server_version(self):
        if self.server_type.currentText() == 'release':
            self.server_version_Specify.setReadOnly(True)
            self.server_version_Specify.setText(self.server_version.currentText())
        else:
            self.server_version_Specify.setReadOnly(False)
            self.server_version_Specify.setText('')

    def update_server(self):
        init_server_path()
        svn_url = None
        if self.server_version_Specify.text() == '':
            QMessageBox.critical(self, "错误", "请填写需要更新的版本")
            return False
        else:
            if self.server_type.currentText() == 'release':
                svn_url = f'http://cd.devsvn.uuzuonline.com/svn/sango_server/ioe_server/release/{self.server_version_Specify.text()}'
            elif self.server_type.currentText() == 'alpha':
                svn_url = f'http://cd.devsvn.uuzuonline.com/svn/sango_server/ioe_server/server/publishOutPut/{self.server_version_Specify.text()}'
            get_server_info(svn_url, self.server_version_Specify.text())
            return True

    def show_progress_dialog(self, func):
        """显示进度条对话框并启动任务"""
        self.progress_dialog = ProgressDialog(func)
        self.progress_dialog.worker.start()  # 启动子线程
        self.progress_dialog.exec()  # 显示对话框（模态）

if __name__ == '__main__':
    # 创建应用和主窗口
    app = QtWidgets.QApplication(sys.argv)
    window = UiServer()
    window.show()

    # 运行应用
    sys.exit(app.exec())
